package top.catblog.notebook.controller;


import org.springframework.web.bind.annotation.*;
import top.catblog.notebook.entity.NoteBook;
import top.catblog.notebook.entity.util.ResponseResult;

import java.util.ArrayList;

@RestController
@RequestMapping
public class NoteBookController {

    // 使用 ArrayList 存储了博客数据
    private final ArrayList<NoteBook> noteBooks = new ArrayList<>();

    // 笔记本：新增一个笔记
    @PostMapping("/notebook/add")
    @SuppressWarnings("unchecked")
    public ResponseResult<NoteBook> add(@RequestBody NoteBook noteBook){
        noteBooks.add(noteBook);
        return ResponseResult.SUCCESS(noteBook);
    }


    // 笔记本：展示全部笔记
    @PostMapping("/notebook/list")
    @SuppressWarnings("unchecked")
    public ResponseResult<ArrayList<NoteBook>> list(){
        return ResponseResult.SUCCESS(noteBooks);
    }

    // 笔记本: 获取笔记本大小
    @PostMapping("/notebook/getSize")
    @SuppressWarnings("unchecked")
    public ResponseResult<Integer> getSize(){
        return ResponseResult.SUCCESS(noteBooks.size());
    }

    // 笔记本：根据ArrayList数组下标获取笔记

    @PostMapping("/notebook/getNotebookByIndex")
    @SuppressWarnings("unchecked")
    public ResponseResult<NoteBook> getNotebookByIndex(@RequestParam int index){
        try {
            NoteBook noteBook = noteBooks.get(index);
            return ResponseResult.SUCCESS(noteBook);
        }catch (Exception e){
            // 此处的e可以选择抛出 或者打印
            return ResponseResult.FAILED("500","数组下标越界了");
        }

    }


}
